package com.helsmanli.holiday.util;

import java.util.ArrayList;
import java.util.List;

import com.lmj.holiday.model.WeekDay;

/**
 * <p>
 * 节假日工具类
 * </p>
 *
 * @author helsmanli
 * @description
 * @date 2022/3/7
 */
public class ChineseHolidaysUtil {
	private static final String SEARCH_LINK = HolidayPropertyUtil.get("search.link");
	private static final String SEARCH_REG_PREFIX = HolidayPropertyUtil.get("search.reg.prefix");
	private static final String SEARCH_REG_SUFFIX = HolidayPropertyUtil.get("search.reg.suffix");
	private static final String HOLIDAY_REG = HolidayPropertyUtil.get("holiday.reg");
	private static final int SEARCH_COUNT = Integer.valueOf(HolidayPropertyUtil.get("search.count"));

	/**
	 * 获取 {year} 年所有的日期 并标注是否为休息日（只包括周六日、不包括节假日及调休）
	 * @param year
	 */
	public static List<WeekDay> getWeekDays(int year) {
		return WeekUtil.getAllDayByYear(year);
	}
	
	/**
	 * 获取国务院办公厅最新发布日期发布年的日期（包括周六日和节假日及调休）
	 * holiday.properties中search.count设置无效
	 * @throws Exception 
	 */
	public static List<WeekDay> getLatestHolidayCalendar () throws Exception{
        List<String> links = LinkUtil.getLinks(SEARCH_LINK, SEARCH_REG_PREFIX + SEARCH_REG_SUFFIX , 1);
        String link = links.get(0);
		return getLatestHolidayCalendarByLink(link);
	}
	
	/**
	 * 获取国务院办公厅最新发布日期发布年的日期（包括周六日和节假日及调休）
	 * holiday.properties中search.count设置无效
	 * @throws Exception 
	 */
	public static List<WeekDay> getLatestHolidayCalendarByLink (String link) throws Exception{
        List<WeekDay> holidays = new ArrayList<>();
        List<List<String>> contents = LinkUtil.getContents(link, HOLIDAY_REG);
        for (List<String> content : contents) {
        	holidays.addAll(EventUtil.getEvents(content, link));
        }
        System.out.println("&&&&&&&&&&&&&&&&&&&&");
        holidays.forEach(h -> System.out.println(h.toString()));
        System.out.println("&&&&&&&&&&&&&&&&&&&&");
		// 获取待发布年份的日期
		List<WeekDay> allDayByYear = WeekUtil.getAllDayByYear(EventUtil.getPubYear(link));
		List<WeekDay> prevYearDate = new ArrayList<WeekDay>();
		for (WeekDay weekDay2 : holidays) {
			boolean flag = false;
			for (WeekDay weekDay : allDayByYear) {
				if (weekDay.getDate().isEqual(weekDay2.getDate())) {
					flag = true;
					weekDay.setDescription(weekDay2.getDescription());
					weekDay.setSummary(weekDay2.getSummary());
					weekDay.setRest(weekDay2.getSummary().contains("补班") ? "0" : "1");
				}
			}
			if (!flag) {
				prevYearDate.add(weekDay2);
			}
		}
		// 将上一年需要补班的日期添加到集合中
		allDayByYear.addAll(0,  prevYearDate);
		return allDayByYear;
	}
	
	/**
	 * 获取最近count次所有节假日及调休日期
	 * @return
	 * @throws Exception 
	 */
	public static List<WeekDay> getHolidayBySearchCount() throws Exception{
        List<WeekDay> events = new ArrayList<>();
        // Get holiday announcement links from search link.
        List<String> links = LinkUtil.getLinks(SEARCH_LINK, SEARCH_REG_PREFIX + SEARCH_REG_SUFFIX , SEARCH_COUNT);
        // Get holiday contents from each holiday announcement link.
        for (String link : links) {
            // System.out.println(link);
            List<List<String>> contents = LinkUtil.getContents(link, HOLIDAY_REG);
            // Get holiday details from each holiday content.
            for (List<String> content : contents) {
                  events.addAll(EventUtil.getEvents(content, link));
            }
        }
        return events;
	}
	
}
